package com.snailhd.common.entity;

import com.snailhd.common.enums.RespCodeEnum;
import lombok.Data;

import java.io.Serializable;


@Data
public class ResultVO<T> implements Serializable {

    private String code;

    private String msg;

    private String subCode;

    private String subMsg;

    private T data;

    public ResultVO() { }

    public ResultVO(IResult<T> iResult) {
        if(iResult.isFail()){
            generateFail(iResult.getSubMsg());
        }else{
            generateSuccess(iResult.getData());
        }
    }

    public ResultVO<T> generateSuccess(){
        return generate(RespCodeEnum.SUCCESS,null);
    }

    public ResultVO<T> generateSuccess(T t){
        this.data = t;
        return generate(RespCodeEnum.SUCCESS,null);
    }


    public ResultVO<T> generateFail(){
        return generate(RespCodeEnum.FAIL,null);
    }

    public ResultVO<T> generateFail(SubMsgResult subMsgResult){
        return generate(RespCodeEnum.FAIL,subMsgResult);
    }


    public ResultVO<T> generate(RespCodeEnum respCodeEnum,SubMsgResult subMsgResult){
        this.code = respCodeEnum.getCode();
        this.msg = respCodeEnum.getMsg();
        if(subMsgResult!=null){
            this.subCode = subMsgResult.getSubCode();
            this.subMsg = subMsgResult.getSubMsg();
        }
        return this;
    }

}
